<template>
  <div>
    <el-table
      :data="state.tableData"
      style="width: 100%"
      max-height="300"
      class="customer-table"
      header-cell-class-name="eltablebg"
    >
      <el-table-column prop="create_time" :label="$t('assets.time')">
        <template #default="r">
          <span>{{ getTimeZoneDate(r.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="currency_name"
        :label="$t('exchange.tradingPair')"
      >
        <template #default="r">
          <span>{{ r.row.currency_name + "/" + r.row.legal_name }}</span>
          <span>{{
            r.row.transaction_type == 1
              ? $t("exchange.marketPrice")
              : r.row.transaction_type == 2
              ? $t("exchange.limitPrice")
              : $t("exchange.stopLoss")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="transaction_direction"
        :label="$t('exchange.direction')"
      >
        <template #default="r">
          <span
            :class="
              r.row.transaction_direction == 1 ? 'text-green' : 'text-red'
            "
            >{{
              r.row.transaction_direction == 1
                ? $t("exchange.buy")
                : $t("exchange.sell")
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="transaction_unit_price"
        :label="$t('exchange.price')"
        ><template #default="r">{{
          $currency(r.row.transaction_unit_price)
        }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="transaction_total_amount"
        :label="$t('assets.amount')"
      >
        <template #default="r">{{
          $currency(r.row.transaction_total_amount)
        }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="transaction_make_amount"
        :label="$t('exchange.dealDone')"
      >
        <template #default="r">
          <span>{{
            r.row.transaction_status == 3
              ? "--"
              : $currency(r.row.transaction_make_amount)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="" :label="$t('exchange.totalTurnover')">
        <template #default="r">
          <span>{{
            r.row.transaction_status == 3
              ? "--"
              : $currency(
                  decimalDigitsInputFilter(
                    r.row.transaction_average_price *
                      r.row.transaction_make_amount,
                    tradingChange.price_accuracy
                  )
                )
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="transaction_average_price"
        :label="$t('exchange.averagePrice')"
        ><template #default="r">
          <span>{{
            r.row.transaction_status == 3
              ? "--"
              : $currency(r.row.transaction_average_price)
          }}</span>
        </template></el-table-column
      >
      <el-table-column prop="" :label="$t('exchange.handlingFee')">
        <template #default="r">
          <span>{{
            r.row.transaction_direction == 1
              ? r.row.transaction_status == 3
                ? "--" + r.row.currency_name
                : dolt(r.row.buy_fee + "") + r.row.currency_name
              : r.row.transaction_status == 3
              ? "--" + r.row.legal_name
              : dolt(r.row.sell_fee + "") + r.row.legal_name
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="right"
        prop="transaction_status"
        :label="$t('assets.status')"
      >
        <template #default="r">
          <span>{{
            r.row.transaction_status == 0
              ? $t("exchange.pengding")
              : r.row.transaction_status == 1
              ? $t("exchange.commissioning")
              : r.row.transaction_status == 2
              ? $t("exchange.completed")
              : $t("exchange.cancelled")
          }}</span>
        </template>
      </el-table-column>
      <template #empty>
        <div>
          <Norecord :useDark="true" />
          <!-- <div class="quotet3 text-12 mb-60 l-10">暂无数据</div> -->
        </div>
      </template>
    </el-table>

    <!-- 监听table滚动条触底 -->
    <Tablescroll @scroll="tableScroll" />
  </div>
</template>

<script>
import Norecord from "@/components/noRecord/NoRecord.vue";
import { transactionRecordPage } from "@/api/currency";
import { demotransactionRecordPage } from "@/api/demo";
import UseSystemSetting from "@/commonapi/useSystemSetting";
import { reactive, computed, onMounted, watch } from "vue";
import { useStore } from "vuex";
import Tablescroll from "@/components/tablescroll/index.vue";
import {
  LoginInterception,
  decimalDigitsInputFilter,
  transferToNumber,
} from "@/utils/common";
import { getTimeZoneDate } from "@/utils/times.js";
export default {
  name: "HistoryCommin",
  components: { Norecord, Tablescroll },
  setup() {
    const $store = useStore();
    const state = reactive({
      tableData: [],
      total: 0,
      listParam: {
        page: 0,
        limit: 10,
      },
    });
    onMounted(() => {
      getList();
    });
    // 获取开关状态
    const { virtual_wallet } = UseSystemSetting();
    const tradingChange = computed(() => {
      return $store.state.exchange.deal_data;
    });
    const dolt = (val) => {
      return transferToNumber(val);
    };
    watch(
      () => virtual_wallet.value,
      () => {
        refresh();
      }
    );
    const getList = () => {
      state.loading = true;
      LoginInterception("/deal")
        .then(() => {
          let setFun = virtual_wallet.value
            ? demotransactionRecordPage
            : transactionRecordPage;
          setFun(state.listParam)
            .then((res) => {
              state.loading = false;
              if (state.listParam.page === 0) {
                state.tableData = res.data.items;
              } else {
                state.tableData = state.tableData.concat(res.data.items);
              }
              state.total = res.data.total;
            })
            .catch(() => {
              state.loading = false;
            });
        })
        .catch(() => {
          state.loading = false;
        });
    };
    const refresh = () => {
      state.listParam.page = 0;
      getList();
    };
    // table滚动条触底
    const tableScroll = () => {
      if (state.total > state.tableData.length) {
        state.listParam.page = state.listParam.page + 1;
        getList();
      }
    };
    return {
      state,
      getList,
      tableScroll,
      decimalDigitsInputFilter,
      tradingChange,
      getTimeZoneDate,
      refresh,
      dolt,
      transferToNumber,
    };
  },
};
</script>

<style scoped lang="less"></style>
